﻿using System.Collections.ObjectModel;
using System.ComponentModel;
using AutoMapper;
using CommunityToolkit.Mvvm.ComponentModel;
using Microsoft.UI.Xaml.Controls;
using WinTime.Data;
using WinTime.Data.Entity;
using WinTime.Models;
using WinTime.Views;

namespace WinTime.ViewModels;

public class MainViewModel : ObservableRecipient
{
    private readonly IMapper _mapper;

    public int maxSec;

    public ObservableCollection<AppModelListDto> AppList = new();


    public List<string> WebList;
    public MainViewModel()
    {
        _mapper = App.GetService<IMapper>();
        WebList = new List<string>();
        LoadList();
    }

    public void LoadList()
    {
        AppList = new();
        using var context = new ApplicationDbContext();
        var allAppModels = context.AppModels.OrderByDescending(x => x.TotalTime).ToList();
        maxSec = allAppModels.FirstOrDefault() == null ? 1 : allAppModels.First().TotalTime;
        if (maxSec == 0) { maxSec = 1; }
        foreach (var appModel in allAppModels)
        {
            var app = _mapper.Map<AppModel, AppModelListDto>(appModel);
            app.ProgressPercent = app.TotalTime * 100 / maxSec;
            app.ProgressPercentWidth = (int)((240) / 100 * app.ProgressPercent);
            AppList.Add(app);
        }
    }
    public void ResizeAppList(double width)
    {
        foreach (var item in AppList)
        {
            item.ProgressPercentWidth = (int)((width - 200) / 100 * item.ProgressPercent);
        }
    }
}
